<?php

//单例模式
class Logger
{
    private static $instance; 
    private $fileHandler;
    private function __construct()
    {
	 //以写方式打开如果存在文件 则清空
	 $this->fileHandler = fopen("c://logger.txt","w");	 
    } 

    public static function GetInstance()
    {
        if(self::$instance == null)
        {
            self::$instance = new Logger();
        } 
        return self::$instance;
    }
	public function Write($data)
	{
	  //写数据时锁定句柄 保证同步
	   $flag=true;
	  if(flock($this->fileHandler,LOCK_EX,$flag))
	  {
		fwrite($this->fileHandler,$data);
		flock($this->fileHandler,LOCK_UN);	  	  	 
	  }
	}
	public function __destruct()
	{
	 fclose($this->fileHandler);
	} 
}

?>
